package com.neulogistics.contrib.warehouse.domain.model.enumeration;

import com.neulogistics.contrib.warehouse.domain.exception.WarehouseDomainException;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.ddd.domain.seedwork.Enumeration;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class CirculationDetail extends Enumeration {
    /**
     * 流动细节: 0:进货入库；1:调拨入库；2:退货入库；3:调拨出库；4:退货出库；5:取货出库
     */
    public static final int Purchase_Inbound_Id = 0;
    public static final int Transfer_Inbound_Id = 1;
    public static final int Return_Inbound_Id = 2;
    public static final int Transfer_Outbound_Id = 3;
    public static final int Return_Outbound_Id = 4;
    public static final int Pick_Up_Outbound_Id = 5;

    public static final CirculationDetail Purchase_Inbound = new CirculationDetail(Purchase_Inbound_Id, "Purchase_Inbound");
    public static final CirculationDetail Transfer_Inbound = new CirculationDetail(Transfer_Inbound_Id, "Transfer_Inbound");
    public static final CirculationDetail Return_Inbound = new CirculationDetail(Return_Inbound_Id, "Return_Inbound");
    public static final CirculationDetail Transfer_Outbound = new CirculationDetail(Transfer_Outbound_Id, "Transfer_Outbound");
    public static final CirculationDetail Return_Outbound = new CirculationDetail(Return_Outbound_Id, "Return_Outbound");
    public static final CirculationDetail Pick_Up_Outbound = new CirculationDetail(Pick_Up_Outbound_Id, "Pick_Up_Outbound");


    protected CirculationDetail(int id, String name) {
        super(id, name);
    }

    private static final List<CirculationDetail> list = Arrays.asList(
            Purchase_Inbound,
            Transfer_Inbound,
            Return_Inbound,
            Transfer_Outbound,
            Return_Outbound,
            Pick_Up_Outbound
    );

    public static List<CirculationDetail> getList(){return list;}

    public static CirculationDetail fromName(String name) throws WarehouseDomainException {
        Optional<CirculationDetail> optional = list.stream().filter(p -> p.name.equals(name)).findFirst();

        if (optional.isPresent()){
            return optional.get();
        }
        else {
            throw new WarehouseDomainException(
                    ErrorCode.OPERATION_ERROR,
                    String.format("Possible values for {%s}: %s",
                            CirculationDetail.class.getSimpleName(),
                            list.stream()
                                    .map(p -> p.name)
                                    .collect(Collectors.joining(",")))
            );
        }
    }

    @Override
    public int compareTo(Object o) {
        return 0;
    }
}
